package com.example.ruanjian2012.controller;


import com.example.ruanjian2012.entity.Carport;
import com.example.ruanjian2012.entity.Table;
import com.example.ruanjian2012.service.CarportService;


import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Random;

@RestController
public class TableController {

    @Autowired
    CarportService cs;

    @RequestMapping("/table")
    public Table<Carport>  getAllCarport(){

        List<Carport> list = cs.list();

        Table<Carport> tab  = new Table<Carport>();
        tab.setData(list);
        tab.setCount(list.size());
        return tab;
    }


    @RequestMapping("/upload")
    public String getUrl(@RequestParam("file") MultipartFile uploadFile
                        ) throws IOException {
        String filename = generatePassword(8).toString();
        File targetFile = new File("C:\\Users\\Administrator\\IdeaProjects\\ruanjian201\\src\\main\\resources\\static\\img", filename+".png");
        FileUtils.writeByteArrayToFile(targetFile, uploadFile.getBytes());
   int i = 0;
   i++;
        return null;
    }

    private static char[] generatePassword(int length) {
        String capitalCaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        String lowerCaseLetters = "abcdefghijklmnopqrstuvwxyz";

        String numbers = "1234567890";
        String combinedChars = capitalCaseLetters + lowerCaseLetters  + numbers;
        Random random = new Random();
        char[] password = new char[length];

        password[0] = lowerCaseLetters.charAt(random.nextInt(lowerCaseLetters.length()));
        password[1] = capitalCaseLetters.charAt(random.nextInt(capitalCaseLetters.length()));

        password[2] = numbers.charAt(random.nextInt(numbers.length()));

        for(int i = 3; i< length ; i++) {
            password[i] = combinedChars.charAt(random.nextInt(combinedChars.length()));
        }
        return password;
    }

}
